package com.wuyan.locationdog.ui.my


import android.text.Editable
import android.text.TextWatcher
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.ext.btnEnable
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.setPwdVisible
import com.arcsoft.commonbase.ext.textStringTrim
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.databinding.MyModifyPwdActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.viewmodel.MyViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MyModifyPwdActivity : BaseBindingActivity<MyModifyPwdActivityBinding>(), TextWatcher {
    private val mViewModel: MyViewModel by viewModels()

    override fun initData() {
        if (DeviceHelp.isDeviceModifyPwd) {
            setTitleText("修改设备登录密码")
        } else {
            setTitleText("修改密码")
        }

        binding.apply {
            etNewPwd.addTextChangedListener(this@MyModifyPwdActivity)
            etNewPwd2.addTextChangedListener(this@MyModifyPwdActivity)
            etPwd.addTextChangedListener(this@MyModifyPwdActivity)

            if (LoginHelp.isDeviceLogin) {
                if (DeviceHelp.selectDeviceBeanContent.deviceDetailVO.userPhone.isNullOrEmpty()) {
                    btnGoPwdPhone.gone()
                }
            }
            btnGoPwdPhone.setOnClickListener {
                ActivityUtils.startActivity(MyModifyPwdPhoneActivity::class.java)

            }
            //密码可见设置
            btnEye.setOnClickListener {
                etPwd.setPwdVisible(it as ImageView)
            }
            btnSure.setOnClickListener {
                if (etNewPwd.textStringTrim() != etNewPwd2.textStringTrim()) {
                    customShowLong("新密码二次输入不一致")
                    return@setOnClickListener
                }

                val map = HashMap<String, Any>()
                map["newPsw"] = etNewPwd.textStringTrim()
                map["oldPsw"] = etPwd.textStringTrim()

                if (DeviceHelp.isDeviceModifyPwd) {
                    map["deviceId"] = DeviceHelp.selectDeviceBeanContent.deviceId
                    mViewModel.updateDevicePwd(map)
                } else {
                    mViewModel.updatePwd(map)
                }


            }
        }
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun afterTextChanged(p0: Editable?) {
        binding.apply {
            btnEnable(btnSure, etPwd, etNewPwd, etNewPwd2)
        }
    }

    override fun observe() {
        super.observe()

        mViewModel.anyBean.observe(this) {
            customShowLong("修改成功")
            lifecycleScope.launch {
                delay(1000)
                if (DeviceHelp.isDeviceModifyPwd) {
                    LoginHelp.logout()
                } else {
                    LoginHelp.logout()
                }
            }

        }
    }

}